package com.driver.controller;

import com.driver.enums.LogType;
import com.driver.pojo.entity.SetLog;
import com.driver.pojo.response.R;
import com.driver.pojo.vo.SetVo;
import com.driver.service.IConfigService;
import com.driver.service.ISetLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/config")
@RequiredArgsConstructor
@Slf4j
public class ConfigController {

    private final IConfigService configService;
    private final ISetLogService setLogService;

    @PostMapping("/getSet")
    public R<SetVo> getSet() {
        SetVo setVo = new SetVo();
        setVo.setVoltage(configService.getVoltage());
        setVo.setRate(configService.getRate());
        setVo.setCode(configService.getCode());
        return R.success(setVo);
    }

    @PostMapping("/setSet")
    public R<Void> setSet(@RequestBody SetVo setVo) {
        configService.setVoltage(setVo.getVoltage());
        configService.setRate(setVo.getRate());
        SetLog setLog = new SetLog();
        setLog.setType(LogType.APPLY);
        setLog.setMessage("操作时间：" + LocalDateTime.now() + "；操作方式：apply；设置电压为" + setVo.getVoltage() + "，设置速率为" + setVo.getRate());
        setLogService.save(setLog);
        return R.success();
    }

    @PostMapping("/setCode")
    public R<Void> setCode(@RequestBody SetVo setVo) {
        configService.setCode(setVo.getCode());
        return R.success();
    }
}
